from django.db import models
from datetime import datetime
from users.models import UserProfile
from courses.models import CourseInfo


# Create your models here.

class UserAsk(models.Model):
    """与用户无关,并且无登录也可以进行咨询"""
    name = models.CharField(max_length=30, verbose_name='姓名')
    phone = models.CharField(max_length=11, verbose_name='手机号码')
    course = models.CharField(max_length=20, verbose_name='课程')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='咨询时间')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '咨询信息'
        verbose_name_plural = verbose_name


class UserLove(models.Model):
    love_man = models.ForeignKey(UserProfile, on_delete=models.SET_NULL, null=True, verbose_name='收藏用户')
    love_id = models.IntegerField(verbose_name='收藏内容的id')
    love_type = models.IntegerField(choices=((1, 'org'), (2, 'course'), (3, 'teacher')), verbose_name='收藏类别')
    love_status = models.BooleanField(default=False, verbose_name='收藏状态')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='收藏时间')

    def __str__(self):
        return self.love_man.username

    class Meta:
        verbose_name = '收藏信息'
        verbose_name_plural = verbose_name


class UserCourse(models.Model):
    study_man = models.ForeignKey(UserProfile, on_delete=models.SET_NULL, null=True, verbose_name='学习用户')
    study_course = models.ForeignKey(CourseInfo, on_delete=models.SET_NULL, null=True, verbose_name='学习课程')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='学习时间')

    def __str__(self):
        return self.study_man.username

    class Meta:
        # 由于课程与用户处于多对多的关系,所以这张表是维护这两者关系的
        # 下面这个字段代表当两者同时重复出现时,不会记录到数据库
        unique_together = ('study_man', 'study_course')
        verbose_name = '用户学习课程信息'
        verbose_name_plural = verbose_name


class UserComment(models.Model):
    comment_man = models.ForeignKey(UserProfile, on_delete=models.SET_NULL, verbose_name='评论用户', null=True)
    comment_course = models.ForeignKey(CourseInfo, on_delete=models.SET_NULL, verbose_name='评论课程', null=True)
    comment_content = models.CharField(max_length=300, verbose_name='评论内容')
    comment_time = models.DateTimeField(default=datetime.now, verbose_name='评论时间')

    def __str__(self):
        return self.comment_man.username

    class Meta:
        verbose_name = '用户评论课程信息'
        verbose_name_plural = verbose_name


class UserMessage(models.Model):
    # 由于数据库中的id是从1开始的,当是0的时候,就给所有人发送消息,当是具体的id时,就代表消息应该发送给谁
    message_man = models.IntegerField(default=0, verbose_name='消息用户')
    message_content = models.CharField(max_length=200, verbose_name='消息内容')
    message_status = models.BooleanField(default=False, verbose_name='消息状态')

    def __str__(self):
        return self.message_content

    class Meta:
        verbose_name = '用户消息信息'
        verbose_name_plural = verbose_name
